//
//  RegisterController.m
//  LifeBox
//
//  Created by Lucky on 2019/11/13.
//  Copyright © 2019 Lucky. All rights reserved.
//

#import "RegisterController.h"

#import "Network+Login.h"
@interface RegisterController ()<UITextFieldDelegate> {
    ///设定倒计时Key
    NSString *keyStr;
}
///手机号码输入
@property (strong, nonatomic) UIView *registerView;
///手机号码输入
@property (strong, nonatomic) UGTextField *phoneTF;
///验证码输入
@property (strong, nonatomic) UGTextField *codeTF;
///密码输入
@property (strong, nonatomic) UGTextField *passwdTF;
///邀请码输入
@property (strong, nonatomic) UGTextField *invitaTF;
///注册Btn
@property (strong, nonatomic) UIButton *registerBtn;
///获取验证码Btn
@property (strong, nonatomic) UIButton *codeBtn;
///关闭按钮
@property (strong, nonatomic) UIButton *clostBtn;
//// 标题lab
@property (strong, nonatomic) UILabel *titlLab;

@end


@implementation RegisterController

#pragma mark - 视图层
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    /*
     * 初始化数据
     */
    keyStr = @"RegisterControllerKey";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationDoit) name:APPTIMEINFORMATION object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.view endEditing:YES];
}

#pragma mark - 创建UI
-(void)createUI {
    self.title = @"注册";
    
    [self.view addSubview:self.clostBtn];
    [self.view addSubview:self.registerView];
    [self.view addSubview:self.titlLab];
    
    /*
     * 分割线1
     */
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = COLOR_EE;
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titlLab.mas_bottom).mas_offset(KAutoAcale(115));
        make.left.mas_equalTo(KAutoAcale(30));
        make.right.mas_equalTo(-KAutoAcale(30));
        make.height.mas_equalTo(1);
    }];
    /*
     * 手机号输入
     */
    _phoneTF = [[UGTextField alloc] init];
    _phoneTF.borderStyle = UITextBorderStyleNone;
    _phoneTF.backgroundColor = UIColor.whiteColor;
    _phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTF.keyboardType = UIKeyboardTypeDefault;
    _phoneTF.font = [UIFont systemFontOfSize:KAutoAcale(15)];
    _phoneTF.placeholder = @"请输入手机号码";
    _phoneTF.delegate = self;
    [self.registerView addSubview:_phoneTF];
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line1.mas_top);
        make.left.mas_equalTo(line1.mas_left).mas_offset(KAutoAcale(5));
        make.right.mas_equalTo(line1.mas_right).mas_offset(-KAutoAcale(5));
        make.height.mas_equalTo(KAutoAcale(40));
    }];
    /*
     * 分割线2
     */
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = COLOR_EE;
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).mas_offset(KAutoAcale(55));
        make.left.right.height.mas_equalTo(line1);
    }];
    /*
     * 获取验证码按钮
     */
    _codeBtn = [[UIButton alloc] init];
    _codeBtn.titleLabel.font = [UIFont systemFontOfSize:KAutoAcale(12)];
    [_codeBtn setTitleColor:COLORPRIMARY forState:UIControlStateNormal];
    [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _codeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.registerView addSubview:_codeBtn];
    [_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line2.mas_top);
        make.right.mas_equalTo(line2.mas_right).mas_offset(-KAutoAcale(5));
        make.width.mas_equalTo(KAutoAcale(140));
        make.height.mas_equalTo(KAutoAcale(40));
    }];
    UG_WEAKSELF
    [_codeBtn bk_addEventHandler:^(id sender) {
        [self.view endEditing:YES];
        if (self.phoneTF.text.length == 0) {
            [self.view ug_msg:@"手机号码不能为空"];
            return;
        }else{
            if (self.phoneTF.text.length < 11) {
                [self.view ug_msg:@"手机号码有误"];
                return;
            }
        }
        [self getCodeHttpWith:weakSelf.phoneTF.text];
    } forControlEvents:UIControlEventTouchUpInside];
    /*
     * 验证码输入框
     */
    _codeTF = [[UGTextField alloc] init];
    _codeTF.borderStyle = UITextBorderStyleNone;
    _codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTF.keyboardType = UIKeyboardTypeNumberPad;
    _codeTF.font = [UIFont systemFontOfSize:KAutoAcale(15)];
    _codeTF.placeholder = @"验证码";
    _codeTF.delegate = self;
    _codeTF.backgroundColor = UIColor.whiteColor;
    [self.registerView addSubview:_codeTF];
    [_codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line2.mas_top);
        make.left.mas_equalTo(line2.mas_left).mas_offset(KAutoAcale(5));
        make.right.mas_equalTo(_codeBtn.mas_left).mas_offset(-KAutoAcale(5));
        make.height.mas_equalTo(KAutoAcale(40));
    }];
    /*
     * 分割线3
     */
    UIView *line3 = [[UIView alloc] init];
    line3.backgroundColor = COLOR_EE;
    [self.registerView addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line2.mas_bottom).mas_offset(KAutoAcale(55));
        make.left.right.height.mas_equalTo(line1);
    }];
    /*
     * 密码输入
     */
    _passwdTF = [[UGTextField alloc] init];
    _passwdTF = [[UGTextField alloc] init];
    _passwdTF.borderStyle = UITextBorderStyleNone;
    _passwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwdTF.keyboardType = UIKeyboardTypeDefault;
    _passwdTF.font = [UIFont systemFontOfSize:KAutoAcale(15)];
    _passwdTF.placeholder = @"设置密码(6-15位数字与字母)";
    _passwdTF.secureTextEntry = YES;
    _passwdTF.delegate = self;
    _passwdTF.backgroundColor = UIColor.whiteColor;
    [self.registerView addSubview:_passwdTF];
    [_passwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line3.mas_top);
        make.left.mas_equalTo(line3.mas_left).mas_offset(KAutoAcale(5));
        make.right.mas_equalTo(line3.mas_right).mas_offset(-KAutoAcale(5));
        make.height.mas_equalTo(KAutoAcale(40));
    }];
    
    /*
     * 邀请码输入
     */
    _invitaTF = [[UGTextField alloc] init];
    _invitaTF.borderStyle = UITextBorderStyleNone;
    _invitaTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _invitaTF.keyboardType = UIKeyboardTypeDefault;
    _invitaTF.font = [UIFont systemFontOfSize:KAutoAcale(15)];
    _invitaTF.placeholder = @"邀请码(选填)";
    _invitaTF.delegate = self;
    _invitaTF.text = @"9OKWQA";
    _invitaTF.backgroundColor = UIColor.whiteColor;
    
    [self.registerView addSubview:_invitaTF];
    
    /*
     * 注册按钮
     */
    _registerBtn = [[UIButton alloc] init];
    _registerBtn.layer.cornerRadius = KAutoAcale(22);
    _registerBtn.backgroundColor = COLORDANGER;
    [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_registerBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [_registerBtn addTarget:self action:@selector(registerBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    _registerBtn.enabled = NO;
    [self.view addSubview:_registerBtn];
    
    /*
     * 已有帐号
     */
    UIButton *existBtn = [[UIButton alloc] init];
    existBtn.titleLabel.font = [UIFont systemFontOfSize:KAutoAcale(12)];
    [existBtn setTitleColor:COLORDANGER forState:UIControlStateNormal];
    [existBtn setTitle:@"已有帐号" forState:UIControlStateNormal];
    [self.view addSubview:existBtn];
    [existBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_registerBtn.mas_bottom).mas_offset(KAutoAcale(15));
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(KAutoAcale(80));
        make.height.mas_equalTo(KAutoAcale(30));
    }];
    [existBtn bk_addEventHandler:^(id sender) {
        [self.navigationController popViewControllerAnimated:NO];
    } forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 注册Btn点击
- (void)registerBtnClicked {
    /*
     * 数据判断
     */
    if (!_passwdTF.text.length<6) {
        [self.view ug_msg:@"密码格式有误"];
        return;
    }
    [self getRegisteredHttp];
}

#pragma mark - 验证码监听
- (void)notificationDoit {
    //    NSInteger codeNum = [AppDelegate getimeIntervalBy:keyStr wirhMax:30];
    //    if (codeNum > 0) {
    //        _codeBtn.userInteractionEnabled = NO;
    //        [_codeBtn setTitle:[NSString stringWithFormat:@"重新获取(%ld)", (long)codeNum] forState:UIControlStateNormal];
    //    }else{
    _codeBtn.userInteractionEnabled = YES;
    [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    //        [AppDelegate removeObjectKey:keyStr];
    //    }
}

#pragma mark - 验证码接口
- (void)getCodeHttpWith:(NSString *)phone {
    [self.view ug_starloading];
    [[Network share] getAuthCode:phone block:^(NSDictionary * _Nullable dataDict, NSError * _Nullable error) {
        [self.view ug_stoploading];
        if (error) {
            [self.view ug_msg:error.domain];
        }else{
            
            //            [AppDelegate addObjectIndateDic:self->keyStr];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationDoit) name:APPTIMEINFORMATION object:nil];
        }
    }];
}

#pragma mark - 注册接口
- (void)getRegisteredHttp {
    [self.view ug_starloading];
    NSString *timeStr = [NSString getCurrentTimeBySecond];
    NSString *tempStr = [timeStr substringFromIndex:timeStr.length - 6];
    NSString *nameStr = [NSString stringWithFormat:@"SHB%@", tempStr];
    [[Network share] registerWithTel:_phoneTF.text passwd:[_passwdTF.text encryptionSha1] authCode:_codeTF.text reccode:_invitaTF.text username:nameStr block:^(NSDictionary * _Nullable dataDict, NSError * _Nullable error) {
        [self.view ug_stoploading];
        if (error) {
            [self.view ug_msg:error.domain];
        }else{
            [self.view ug_msg:@"注册成功"];
            [self.navigationController popViewControllerAnimated:NO];
        }
    }];
}



#pragma mark - get
-(UIButton *)clostBtn{
    if (!_clostBtn) {
        _clostBtn = [UIButton new];
        [_clostBtn setBackgroundImage:[UIImage imageWithIcon:@"fa-times" backgroundColor:UIColor.clearColor iconColor:COLORINFO andSize:CGSizeMake(KAutoAcale(32), KAutoAcale(32))] forState:UIControlStateNormal];
        [_clostBtn bk_addEventHandler:^(id sender) {
            [self.navigationController popViewControllerAnimated:YES];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _clostBtn;
}

-(UIView *)registerView{
    if (!_registerView) {
        _registerView = [UIView new];
        _registerView.backgroundColor = UIColor.whiteColor;
        [_registerView ug_radius:KAutoAcale(4)];
    }
    return _registerView;
}
-(UILabel *)titlLab{
    if (!_titlLab) {
        _titlLab = [UILabel new];
        _titlLab.font = [UIFont systemFontOfSize:KAutoAcale(30)];
        _titlLab.textColor = COLOR_23;
        _titlLab.text = @"手机号注册";
    }
    return _titlLab;
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_titlLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(KAutoAcale(30));
        make.top.mas_equalTo(self.clostBtn.mas_bottom).mas_offset(KAutoAcale(65));
        make.width.height.mas_greaterThanOrEqualTo(10);
    }];
    [self.clostBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(KAutoAcale(25));
        make.top.mas_equalTo(KSAFE_Top+ KAutoAcale(10));
        make.width.height.mas_equalTo(KAutoAcale(20));
    }];
    [_invitaTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passwdTF.mas_bottom).mas_offset(KAutoAcale(15));
        make.left.mas_equalTo(self.registerView).mas_offset(KAutoAcale(5));
        make.right.mas_equalTo(self.registerView).mas_offset(-KAutoAcale(5));
        make.height.mas_equalTo(KAutoAcale(40));
    }];
    
    [_registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneTF).mas_offset(-KPAND_DEF);
        make.left.mas_equalTo(KAutoAcale(30));
        make.right.mas_equalTo(-KAutoAcale(30));
        make.bottom.mas_equalTo(self.invitaTF).mas_offset(KPAND_DEF);
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.registerView.mas_bottom).mas_offset(KAutoAcale(40));
        make.left.mas_equalTo(KAutoAcale(30));
        make.right.mas_equalTo(-KAutoAcale(30));
        make.height.mas_equalTo(KAutoAcale(45));
    }];
    
}

@end
